package com.etone.smartAudit.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.CertificatePersonInfo;
import com.etone.smartAudit.dto.certificate.AddHandPersonDTO;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.CertificatePersonInfoMapper;
import com.etone.smartAudit.vo.certificate.HandPersonVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存证单执行人信息  服务实现类
 * </p>
 *
 * @author jiayen
 * @since 2021-06-08
 */
@Service
public class CertificatePersonInfoService extends ServiceImpl<CertificatePersonInfoMapper, CertificatePersonInfo> {


    /***
     * 添加执行人
     * @param dto
     */
    public CertificatePersonInfo addPerson(AddHandPersonDTO dto) {
        //判断该信息用户是否存在
        LambdaQueryWrapper<CertificatePersonInfo> query = new LambdaQueryWrapper<>();
        query.eq(CertificatePersonInfo::getName, dto.getHandPersonName());
        query.eq(CertificatePersonInfo::getPhone, dto.getHandPersonPhone());
        query.eq(CertificatePersonInfo::getEmail, dto.getHandPersonEamil());
//        if (CollectionUtil.isNotEmpty(this.list(query))) {
//            throw new CustomParameterizedException("该用户信息已经存在");
//        }
        CertificatePersonInfo person = new CertificatePersonInfo();
        person.setName(dto.getHandPersonName());
        person.setPhone(dto.getHandPersonPhone());
        person.setEmail(dto.getHandPersonEamil());
        person.setUpdateTime(LocalDateTime.now());
        person.setType(dto.getType());
        person.setCreateTime(LocalDateTime.now());
        this.save(person);
        return person;
    }

    /***
     * 显示执行人
     * @return
     */
    public List<HandPersonVo> listPersons(Integer type) {
        LambdaQueryWrapper<CertificatePersonInfo> query = new LambdaQueryWrapper<>();
        query.eq(CertificatePersonInfo::getType, type);
        List<CertificatePersonInfo> persons = this.list(query);
        List<HandPersonVo> list = new ArrayList<>();
        persons.stream().forEach(person -> {
            HandPersonVo vo = new HandPersonVo();
            BeanUtils.copyProperties(person, vo);
            list.add(vo);
        });
        return list;
    }

    /***
     * 根据电话号码获取执行人，不存在则添加
     * @param mobile
     * @return
     */
    public CertificatePersonInfo getBymobile(String mobile) {
        LambdaQueryWrapper<CertificatePersonInfo> query = new LambdaQueryWrapper<>();
        query.eq(CertificatePersonInfo::getPhone, mobile);
        List<CertificatePersonInfo> list = this.list(query);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        if (list.size() > 1) {
            //说明有多个，删除所有,重新添加
            this.remove(query);
            return null;
        }
        return list.get(0);
    }
}
